package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import att.grappa.Node;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/3/11
 * Time: 9:47 AM
 * To change this template use File | Settings | File Templates.
 */
public class RenameTaskListener implements ActionListener
{

    String job;
    GraphVizGraph graphVizObject;
    Connection con;
    EditIntFrame editIntFrame;


    public RenameTaskListener(String job, GraphVizGraph graphVizObject, Connection con, EditIntFrame editIntFrame)
    {
        this.job = job;
        this.graphVizObject = graphVizObject;
        this.con = con;
        this.editIntFrame = editIntFrame;
    }


    public void actionPerformed(ActionEvent e)
    {


        Node[] nodes = graphVizObject.getGraph().nodeElementsAsArray();
        String[] currentNodeNames = new String[nodes.length];
        for (int i = 0; i < nodes.length; i++)
        {
            currentNodeNames[i] = nodes[i].getName();

        }
        Arrays.sort(currentNodeNames);
        JComboBox cbox1 = new JComboBox(currentNodeNames);
        JTextField newName = new JTextField();

        Object[] message = new Object[]{"Task_Tag", cbox1, "New Name", newName};
        int input = JOptionPane.showConfirmDialog(null, message, "ENTER NEW NAME FOR THE TASK", JOptionPane.OK_CANCEL_OPTION);
        if (input == JOptionPane.OK_OPTION)
        {
            String taskTag = cbox1.getSelectedItem().toString();
            String newTask = newName.getText();

            CallStoredProc storedProc = new CallStoredProc("{call tms.task_util.rename(?,?,?)}", new String[]{job, taskTag, newTask}, con);

            editIntFrame.sqlCommands.add(storedProc);
            editIntFrame.model.addElement(storedProc);
            editIntFrame.sqlcommandsList.setModel(editIntFrame.model);
        }

    }

}